package com.glodon.paas.activity.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.velocity.VelocityContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.glodon.paas.activity.api.bean.Activity;
import com.glodon.paas.activity.dao.domain.ActivityEntity;
import com.glodon.paas.exception.PaasServerExceptionFactory;

public class VelocityContextWrapActivity extends VelocityContext {
    private static Logger LOGGER = LoggerFactory.getLogger(VelocityContextWrapActivity.class);

    private static String       PRE   = "extension";
    private static Utils        utils = new Utils();

    @SuppressWarnings("unchecked")
    public static VelocityContext wrap(ActivityEntity contextObject) {
        try {
            Map<String, Object> map1 = BeanUtils.describe(contextObject);

            VelocityContext context = new VelocityContext(map1);
            context.put("innerObj", contextObject);
            context.put("utils", utils);
            if (contextObject.getExtension() != null) {
                context.put(PRE, new VelocityContext(contextObject.getExtension()));
            }

            return context;
        } catch (Exception e) {
            throw PaasServerExceptionFactory.systemException("BeanUtils.describe(ActivityEntity) error", e);
        }
    }
    
    @SuppressWarnings("unchecked")
    public static VelocityContext wrap(Activity contextObject) {
        try {
            Map<String, Object> map1 = BeanUtils.describe(contextObject);

            VelocityContext context = new VelocityContext(map1);
            context.put("innerObj", contextObject);
            context.put("utils", utils);
            if (contextObject.getExtension() != null) {
                context.put(PRE, new VelocityContext(contextObject.getExtension()));
            }

            return context;
        } catch (Exception e) {
            throw PaasServerExceptionFactory.systemException("BeanUtils.describe(Activity) error", e);
        }
    }

    static public class Utils {
        /**
         * 从输入集合中获取以prefix开头的key集合，并按字符序列排序
         * @param prefix
         * @return
         */
        public List<String> subKeys(Set<String> set, String prefix){
            List<String> list = new ArrayList<String>();
            for(String key : set){
                if(key.startsWith(prefix)){
                    list.add(key);
                }
            }
            Collections.sort(list);
            return list;
        }
        
        /**
         * 从start开始，每次递增step，得到count个数字序列
         * 
         * @param prefix
         * @return
         */
        public List<Integer> numberArrays(int start, int step, Object count) {
            Integer c = 0;
            try {
                c = Integer.parseInt(count.toString());
            } catch (Exception e) {
                LOGGER.error("numberArrays error:count="+count);
                return new ArrayList<Integer>();
            }

            List<Integer> list = new ArrayList<Integer>();
            for (int i = 0; i < c; i++) {
                list.add(start);
                start += step;
            }
            return list;
        }
        
        private static HashMap<String,Object> clazz = new HashMap<String,Object>();
        @SuppressWarnings("unchecked")
        public Map<String,Object> jsonToMap(String str) {
            return JSON.parseObject(str, clazz.getClass());
        }
    }
}
